import 'package:flutter_base/common/apis/apis.dart';
import 'package:flutter_base/common/entitys/entitys.dart';
import 'package:flutter_base/common/utils/utils.dart';
import 'package:flutter_base/common/values/values.dart';
import 'package:flutter_base/common/widgets/widgets.dart';
import 'package:flutter_base/global.dart';
import 'package:flutter/material.dart';

class SignInPage extends StatefulWidget {
  SignInPage({Key? key}) : super(key: key);

  @override
  _SignInPageState createState() => _SignInPageState();
}

class _SignInPageState extends State<SignInPage> {
  //email的控制器
  final TextEditingController _userNameController = TextEditingController();

  //密码的控制器
  final TextEditingController _passController = TextEditingController();

  // logo
  Widget _buildLogo() {
    return Container(
      width: zhiSetWidth(110),
      margin: EdgeInsets.only(top: zhiSetHeight(40 + 44.0)), // 顶部系统栏 44px
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Container(
            height: zhiSetWidth(76),
            width: zhiSetWidth(76),
            margin: EdgeInsets.symmetric(horizontal: zhiSetWidth(15)),
            child: Stack(
              alignment: Alignment.center,
              children: [
                Positioned(
                  left: 0,
                  top: 0,
                  right: 0,
                  child: Container(
                    height: zhiSetWidth(76),
                    decoration: BoxDecoration(
                      color: AppColors.primaryBackground,
                      boxShadow: [
                        Shadows.primaryShadow,
                      ],
                      borderRadius: BorderRadius.all(
                          Radius.circular(zhiSetWidth(76 * 0.5))), // 父容器的50%
                    ),
                    child: Container(),
                  ),
                ),
                Positioned(
                  top: zhiSetWidth(13),
                  child: Image.asset(
                    "assets/images/logo.png",
                    fit: BoxFit.none,
                  ),
                ),
              ],
            ),
          ),
          Container(
            margin: EdgeInsets.only(top: zhiSetHeight(15)),
            child: Text(
              "MIIC",
              textAlign: TextAlign.center,
              style: TextStyle(
                color: AppColors.primaryText,
                fontFamily: "Montserrat",
                fontWeight: FontWeight.w600,
                fontSize: zhiSetFontSize(24),
                height: 1,
              ),
            ),
          ),
          Text(
            " ",
            textAlign: TextAlign.center,
            style: TextStyle(
              color: AppColors.primaryText,
              fontFamily: "Avenir",
              fontWeight: FontWeight.w400,
              fontSize: zhiSetFontSize(16),
              height: 1,
            ),
          ),
        ],
      ),
    );
  }

  // 登录表单
  Widget _buildInputForm() {
    return Container(
      width: zhiSetWidth(295),
      // height: 204,
      margin: EdgeInsets.only(top: zhiSetHeight(49)),
      child: Column(
        children: [
          // email input
          inputTextEdit(
            controller: _userNameController,
            // keyboardType: TextInputType.emailAddress,
            hintText: "Username",
            marginTop: 0,
          ),
          // password input
          inputTextEdit(
            controller: _passController,
            keyboardType: TextInputType.visiblePassword,
            hintText: "Password",
            isPassword: true,
          ),

          // 注册、登录 横向布局
          Container(
            height: zhiSetHeight(44),
            margin: EdgeInsets.only(top: zhiSetHeight(15)),
            child: Row(
              children: [
                // 注册
                btnFlatButtonWidget(
                  onPressed: () {
                    Navigator.pushNamed(
                      context,
                      "/sign-up",
                    );
                  },
                  gbColor: AppColors.thirdElement,
                  title: "Sign up",
                ),
                Spacer(),
                // 登录
                btnFlatButtonWidget(
                  onPressed: () async {
                    // if (!duIsEmail(_emailController.value.text)) {
                    //   toastInfo(msg: '请正确输入邮件');
                    //   return;
                    // }
                    // if (!zhiCheckStringLength(
                    //     _userNameController.value.text, 0)) {
                    //   toastInfo(msg: '请输入用户名');
                    //   return;
                    // }
                    // if (!zhiCheckStringLength(
                    //     _passController.value.text, 6)) {
                    //   toastInfo(msg: '密码不能小于6位');
                    //   return;
                    // }
                    //
                    // UserRequestEntity params = UserRequestEntity(
                    //     username: _emailController.value.text,
                    //     password: duSHA256(_passController.value.text),
                    //     deviceId: '123');
                    //
                    // UserRequestEntity params = UserRequestEntity(
                    //     username: _userNameController.value.text,
                    //     password: zhiMd5(zhiMd5(_passController.value.text)),
                    //     rememberMe: true,
                    //     webSource: 'webSource',
                    //     code: '3944',
                    //     deviceId: 'mobile');
                    // print(params.toJson());
                    // UserResponseEntity userProfile =
                    //     await UserAPI.login(params: params, context: context);
                    // if (userProfile.code == 200) {
                    //   toastInfo(msg: '登录成功');
                    // }
                    // Global.saveProfile(userProfile);
                    // print(userProfile.token);
                    // print(Global.profile.token);

                    Navigator.pushNamedAndRemoveUntil(
                        context, "/app", (route) => false);
                    // 写本地 access_token , 不写全局，业务：离线登录
                    // 全局数据 gUser
                    // List<CategoryResponseEntity> res = await NewsAPI.categories();
                    // print(res.length);
                  },
                  gbColor: AppColors.primaryElement,
                  title: "Sign in",
                ),
              ],
            ),
          ),
          // Spacer(),

          // Fogot password
          Container(
            height: zhiSetHeight(22),
            margin: EdgeInsets.only(top: zhiSetHeight(20)),
            child: FlatButton(
              onPressed: () => {},
              child: Text(
                "Fogot password?",
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: AppColors.secondaryElementText,
                  fontFamily: "Avenir",
                  fontWeight: FontWeight.w400,
                  fontSize: zhiSetFontSize(16),
                  height: 1, // 设置下行高，否则字体下沉
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  // 第三方登录
  Widget _buildThirdPartyLogin() {
    return Container(
      width: zhiSetWidth(295),
      margin: EdgeInsets.only(bottom: zhiSetHeight(40)),
      child: Column(
        children: <Widget>[
          // title
          Text(
            "Or sign in with social networks",
            textAlign: TextAlign.center,
            style: TextStyle(
              color: AppColors.primaryText,
              fontFamily: "Avenir",
              fontWeight: FontWeight.w400,
              fontSize: zhiSetFontSize(16),
            ),
          ),
          // 按钮
          Padding(
            padding: EdgeInsets.only(top: zhiSetHeight(20)),
            child: Row(
              children: <Widget>[
                btnFlatButtonBorderOnlyWidget(
                  onPressed: () {},
                  width: 88,
                  iconFileName: "twitter",
                ),
                Spacer(),
                btnFlatButtonBorderOnlyWidget(
                  onPressed: () {},
                  width: 88,
                  iconFileName: "google",
                ),
                Spacer(),
                btnFlatButtonBorderOnlyWidget(
                  onPressed: () {},
                  width: 88,
                  iconFileName: "facebook",
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  // 注册按钮
  Widget _buildSignupButton() {
    return Container(
      margin: EdgeInsets.only(bottom: zhiSetHeight(20)),
      child: btnFlatButtonWidget(
        onPressed: () {},
        width: 294,
        gbColor: AppColors.secondaryElement,
        fontColor: AppColors.primaryText,
        title: "Sign up",
        fontWeight: FontWeight.w500,
        fontSize: 16,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: Center(
        child: Column(
          children: <Widget>[
            _buildLogo(),
            _buildInputForm(),
            Spacer(),
            // _buildThirdPartyLogin(),
            // _buildSignupButton(),
          ],
        ),
      ),
    );
  }
}
